package com.meilai.project.controller.web.report;

import cn.hutool.core.collection.CollUtil;
import com.meilai.project.constant.AppUserTypeEnum;
import com.meilai.project.controller.web.report.dto.*;
import com.meilai.project.controller.web.report.entity.AppNewInstallWorkOrderReport;
import com.meilai.project.controller.web.report.entity.AppWorkOrderReport;
import com.meilai.project.controller.web.report.entity.BillMarketShootRequirementExt;
import com.meilai.project.controller.web.report.entity.TypeCount;
import com.meilai.project.controller.web.report.helper.WorkOrderHelper;
import com.meilai.project.controller.web.report.mapper.WorkOrderReportMapper;
import com.meilai.project.controller.web.report.vo.*;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.entity.business.installOrder.WorkOrder;
import com.meilai.project.entity.system.User;
import com.meilai.project.service.business.installOrder.WorkOrderService;
import com.meilai.project.service.system.UserService;
import com.meilai.project.util.DateUtil;
import com.meilai.project.util.ThreadLocalUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenxing
 */
@RestController
@RequestMapping("api/app/work-order-report")
@Api(tags = "报表")
public class AppWorkOrderReportController {

    @Autowired
    private WorkOrderReportMapper reportMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private WorkOrderService workOrderService;

    @PostMapping("summary")
    @ApiOperation(value = "总览（个人+管理）")
    public ApiResult<AppSummaryVO> summary(@ApiParam("json") @RequestBody AppSummaryReq req) {
        req.setManageDefaultNull(true);
        alterReqForUserId(req);

        AppSummaryVO summaryVO = new AppSummaryVO();
        List<Integer> summaryTotalWorkOrderIdList = reportMapper.summaryTotalCount(req.getDateRangeType(), req.getUserId(), null);
        List<Integer> summarySubmitWorkOrderIdList = reportMapper.summarySubmitCount(req.getDateRangeType(), req.getUserId());
        summaryVO.setSubmitWorkOrderCount(summarySubmitWorkOrderIdList.size());
        summaryVO.setTotalWorkOrderCount(summaryTotalWorkOrderIdList.size());
        if (summaryTotalWorkOrderIdList.size() > 0) {
            List<WorkOrder> workOrders = workOrderService.listByIds(summaryTotalWorkOrderIdList);
            List<Long> billSaleIdList = workOrders.stream().map(WorkOrder::getBill_sale_id).filter(Objects::nonNull).collect(Collectors.toList());
            List<Long> workOrderIdList = workOrders.stream().filter(item -> item.getBill_sale_id() == null).map(WorkOrder::getId).collect(Collectors.toList());
            int total = 0;
            if (billSaleIdList.size() > 0) {
                Integer sum = reportMapper.summarySaleMediaPointCount(billSaleIdList);
                total = total + (null == sum ? 0 : sum);
            }
            if (workOrderIdList.size() > 0) {
                Integer sum = reportMapper.summaryWorkOrderMediaPointCount(workOrderIdList);
                total = total + (null == sum ? 0 : sum);
            }
            if (workOrderIdList.size() > 0) {
                Integer sum = reportMapper.summaryWorkOrderInstallMediaPointCount(workOrderIdList);
                total = total + (null == sum ? 0 : sum);
            }
            Integer submitMediaPointCount = reportMapper.summaryHasSubmitMediaPointCount(summaryTotalWorkOrderIdList);
            summaryVO.setSubmitMediaPointCount(submitMediaPointCount);
            summaryVO.setTotalMediaPointCount(total);

        }
        return ApiResult.ok(summaryVO);
    }

    @PostMapping("summary-by-work-order-type")
    @ApiOperation("总览（按工单类型）")
    public ApiResult<List<AppSummaryByWorkOrderTypeVO>> summaryByWorkOrderType(@ApiParam("json") @RequestBody AppSummaryByWorkOrderTypeReq req) {
        alterReqForUserId(req);

        List<AppSummaryByWorkOrderTypeVO> result = new ArrayList<>();

        List<TypeCount> summaryTypeTotalCountList = reportMapper.summaryTypeTotalCount(req.getDateRangeType(), req.getUserId());
        List<TypeCount> summaryTypeSubmitCountList = reportMapper.summaryTypeSubmitCount(req.getDateRangeType(), req.getUserId());
        List<TypeCount> summaryTypeBuildingCountList = reportMapper.summaryTypeBuildingCount(req.getDateRangeType(), req.getUserId());
        List<TypeCount> summaryTypeCustomerCountList = reportMapper.summaryTypeCustomerCount(req.getDateRangeType(), req.getUserId());
        Map<Integer, Integer> totalCountMap = type2CountMap(summaryTypeTotalCountList);
        Map<Integer, Integer> submitCountMap = type2CountMap(summaryTypeSubmitCountList);
        Map<Integer, Integer> buildingCountMap = type2CountMap(summaryTypeBuildingCountList);
        Map<Integer, Integer> customerCountMap = type2CountMap(summaryTypeCustomerCountList);
        List<Integer> typeIds = WorkOrderHelper.typeIds();
        for (Integer typeId : typeIds) {
            AppSummaryByWorkOrderTypeVO item = new AppSummaryByWorkOrderTypeVO();
            item.setWork_order_type_id(typeId);
            item.setWork_order_type_name(WorkOrderHelper.getWorkOrderName(typeId));
            item.setTotal_work_order_count(totalCountMap.get(typeId));
            item.setSubmit_work_order_count(submitCountMap.get(typeId));
            item.setTotal_building_count(buildingCountMap.get(typeId));
            item.setTotal_customer_count(customerCountMap.get(typeId));
            result.add(item);
        }
        return ApiResult.ok(result);
    }

    @PostMapping("summary-by-worker")
    @ApiOperation("总览（按工人）")
    public ApiResult<List<AppSummaryByWorkerVO>> summaryByWorker(@ApiParam("json") @RequestBody AppSummaryByWorkerReq req) {
        List<AppSummaryByWorkerVO> result = new ArrayList<>();

        List<TypeCount> summaryTypeTotalCountList = reportMapper.summaryPersonTotalCount(req.getDateRangeType());
        List<TypeCount> summaryTypeSubmitCountList = reportMapper.summaryPersonSubmitCount(req.getDateRangeType());
        List<TypeCount> summaryTypeBuildingCountList = reportMapper.summaryPersonBuildingCount(req.getDateRangeType());
        List<TypeCount> summaryTypeCustomerCountList = reportMapper.summaryPersonCustomerCount(req.getDateRangeType());
        Map<Integer, Integer> totalCountMap = type2CountMap(summaryTypeTotalCountList);
        Map<Integer, Integer> submitCountMap = type2CountMap(summaryTypeSubmitCountList);
        Map<Integer, Integer> buildingCountMap = type2CountMap(summaryTypeBuildingCountList);
        Map<Integer, Integer> customerCountMap = type2CountMap(summaryTypeCustomerCountList);
        List<User> userList = userService.list_by_role_code("MarketingInstaller");
        for (User user : userList) {
            AppSummaryByWorkerVO item = new AppSummaryByWorkerVO();
            int typeId = user.getId().intValue();
            item.setWorker_id((long) typeId);
            item.setWorker_name(user.getName());
            item.setTotal_work_order_count(totalCountMap.get(typeId));
            item.setSubmit_work_order_count(submitCountMap.get(typeId));
            item.setTotal_building_count(buildingCountMap.get(typeId));
            item.setTotal_customer_count(customerCountMap.get(typeId));
            result.add(item);
        }
        return ApiResult.ok(result);
    }

    private Map<Integer, Integer> type2CountMap(List<TypeCount> countList) {
        return countList.stream().collect(Collectors.toMap(TypeCount::getType_id, TypeCount::getItem_count));
    }

    @PostMapping("work-order-process-building")
    @ApiOperation("进度详情(楼盘相关)")
    public ApiResult<List<AppWorkOrderBuildingProcessVO>> workOrderProcessBuilding(@ApiParam("json") @RequestBody AppWorkOrderProcessReq req) {
        req.setManageDefaultNull(true);
        alterReqForUserId(req);

        List<AppWorkOrderReport> buildingProcessList = null;
        if (null != req.getWork_order_type_id()) {
            if (req.getWork_order_type_id() == 1) {
                List<Long> workOrderIdList = reportMapper.workOrderBuildingNewInstallProcessIdList(req.getDateRangeType(), req.getUserId(), req.getWork_order_type_id(), req.getMedia_type_id());
                List<AppWorkOrderBuildingProcessVO> result = new ArrayList<>();
                if (CollUtil.isNotEmpty(workOrderIdList)) {
                    List<AppNewInstallWorkOrderReport> installReportList = reportMapper.workOrderBuildingNewInstallProcess(
                            req.getDateRangeType(), req.getUserId(), req.getWork_order_type_id(), req.getMedia_type_id(), workOrderIdList);
                    for (AppNewInstallWorkOrderReport appWorkOrderReport : installReportList) {
                        AppWorkOrderBuildingProcessVO item = new AppWorkOrderBuildingProcessVO();
                        item.setWork_order_type_id(appWorkOrderReport.getWork_order_type_id());
                        item.setWork_order_type_name(WorkOrderHelper.getWorkOrderName(appWorkOrderReport.getWork_order_type_id()));
                        item.setBuilding_area_id(appWorkOrderReport.getBuilding_area_id());
                        item.setBuilding_area_name(appWorkOrderReport.getBuilding_area_name());
                        item.setHasSubmitCount(appWorkOrderReport.getSubmit_num());
                        item.setHasNotSubmitCount(appWorkOrderReport.getSubmit_num() == null ? appWorkOrderReport.getTotal_num() : appWorkOrderReport.getTotal_num() - appWorkOrderReport.getSubmit_num());
                        result.add(item);
                    }
                }
                return ApiResult.ok(result);
            } else {
                buildingProcessList = reportMapper.workOrderBuildingProcess(
                        req.getDateRangeType(), req.getUserId(), req.getWork_order_type_id(), req.getMedia_type_id());
                List<AppWorkOrderBuildingProcessVO> buildingProcessVOList = assembleBuildingProcessList(buildingProcessList, true);
                return ApiResult.ok(buildingProcessVOList);
            }

        } else {
            List<Long> workOrderIdList = reportMapper.workOrderBuildingNewInstallProcessIdList(
                    req.getDateRangeType(), req.getUserId(), 1L, req.getMedia_type_id());
            List<AppWorkOrderBuildingProcessVO> result = new ArrayList<>();
            if (CollUtil.isNotEmpty(workOrderIdList)) {
                List<AppNewInstallWorkOrderReport> installReportList = reportMapper.workOrderBuildingNewInstallProcess(
                        req.getDateRangeType(), req.getUserId(), 1L, req.getMedia_type_id(), workOrderIdList);

                for (AppNewInstallWorkOrderReport appWorkOrderReport : installReportList) {
                    AppWorkOrderBuildingProcessVO item = new AppWorkOrderBuildingProcessVO();
                    item.setWork_order_type_id(appWorkOrderReport.getWork_order_type_id());
                    item.setWork_order_type_name(WorkOrderHelper.getWorkOrderName(appWorkOrderReport.getWork_order_type_id()));
                    item.setBuilding_area_id(appWorkOrderReport.getBuilding_area_id());
                    item.setBuilding_area_name(appWorkOrderReport.getBuilding_area_name());
                    item.setHasSubmitCount(appWorkOrderReport.getSubmit_num());
                    item.setHasNotSubmitCount(appWorkOrderReport.getSubmit_num() == null ? appWorkOrderReport.getTotal_num() : appWorkOrderReport.getTotal_num() - appWorkOrderReport.getSubmit_num());
                    result.add(item);
                }

                buildingProcessList = reportMapper.workOrderBuildingProcess(
                        req.getDateRangeType(), req.getUserId(), req.getWork_order_type_id(), req.getMedia_type_id());
                List<AppWorkOrderBuildingProcessVO> buildingProcessVOList = assembleBuildingProcessList(buildingProcessList, true);
                result.addAll(buildingProcessVOList);
            }
            return ApiResult.ok(result);
        }
    }

    @PostMapping("work-order-process-customer")
    @ApiOperation("进度详情(客户相关)")
    public ApiResult<List<AppWorkOrderCustomerProcessVO>> workOrderProcessCustomer(@ApiParam("json") @RequestBody AppWorkOrderProcessReq req) {
        req.setManageDefaultNull(true);
        alterReqForUserId(req);

        List<AppWorkOrderReport> customerProcessList = reportMapper.workOrderCustomerProcess(req.getDateRangeType()
                , req.getUserId(), req.getWork_order_type_id(), req.getMedia_type_id());
        List<AppWorkOrderCustomerProcessVO> customerProcessVOList = assembleCustomerProcessList(customerProcessList);
        return ApiResult.ok(customerProcessVOList);
    }


    @PostMapping("work-order-process-detail")
    @ApiOperation("进度详情-未提交")
    public ApiResult<List<AppWorkOrderProcessDetailVO>> workOrderProcessDetail(@ApiParam("json") @RequestBody AppWorkOrderProcessDetailReq req) {
        req.setManageDefaultNull(true);
        alterReqForUserId(req);

        List<Long> notSubmitMediaPointIdList;
        if (req.getWork_order_id() == null) {
            notSubmitMediaPointIdList = reportMapper.workOrderProcessBuildingNotSubmitMediaPointIdList(req.getDateRangeType(), req.getUserId(), req.getBuilding_area_id(), req.getWork_order_type_id());
        } else {
            notSubmitMediaPointIdList = reportMapper.workOrderProcessCustomerNotSubmitMediaPointIdList(req.getDateRangeType(), req.getUserId(), req.getBuilding_area_id(), req.getWork_order_type_id(), req.getWork_order_id());
        }
        if (CollUtil.isEmpty(notSubmitMediaPointIdList)) {
            return ApiResult.ok(new ArrayList<>());
        }
        List<AppWorkOrderProcessDetailVO> result = reportMapper.workOrderProcessDetail(notSubmitMediaPointIdList);
        return ApiResult.ok(result);
    }

    @PostMapping("work-order-media-type-process")
    @ApiOperation("工单详情")
    public ApiResult<List<AppWorkerProcessVO>> workTypeProcess(@ApiParam("json") @RequestBody AppWorkerProcessReq req) {
        alterReqForUserId(req);
        List<AppWorkerProcessVO> result = new ArrayList<>();
        List<Integer> allWorkOrderIdList = reportMapper.summaryTotalCount(req.getDateRangeType(),
                req.getUserId(), req.getWork_order_type_id());
        if (allWorkOrderIdList.size() > 0) {
            List<WorkOrder> workOrders = workOrderService.listByIds(allWorkOrderIdList);
            List<Long> billSaleWorkOrderIdList = workOrders.stream().filter(item -> item.getBill_sale_id() != null).map(WorkOrder::getId).collect(Collectors.toList());
            List<Long> workOrderIdList = workOrders.stream().filter(item -> item.getBill_sale_id() == null).map(WorkOrder::getId).collect(Collectors.toList());
            List<AppWorkerProcessVO> processVOList = new ArrayList<>();
            if (CollUtil.isNotEmpty(workOrderIdList)) {
                List<AppWorkerProcessVO> mediaTypeWorkOrderCount;
                if (req.getWork_order_type_id() != null) {
                    if (req.getWork_order_type_id() == 1) {
                        mediaTypeWorkOrderCount = reportMapper.mediaTypeNewInstallWorkOrderCount(workOrderIdList);
                    } else {
                        mediaTypeWorkOrderCount = reportMapper.mediaTypeWorkOrderCount(workOrderIdList);
                    }
                    processVOList.addAll(mediaTypeWorkOrderCount);
                } else {
                    List<AppWorkerProcessVO> mediaTypeNewInstallWorkOrderCount = reportMapper.mediaTypeNewInstallWorkOrderCount(workOrderIdList);
                    List<AppWorkerProcessVO> mediaTypeWorkOrderCount1 = reportMapper.mediaTypeWorkOrderCount(workOrderIdList);
                    processVOList.addAll(mediaTypeNewInstallWorkOrderCount);
                    processVOList.addAll(mediaTypeWorkOrderCount1);
                }
            }
            if (CollUtil.isNotEmpty(billSaleWorkOrderIdList)) {
                List<AppWorkerProcessVO> mediaTypeSaleCount = reportMapper.mediaTypeSaleCount(billSaleWorkOrderIdList);
                processVOList.addAll(mediaTypeSaleCount);
            }

            Map<Integer, List<AppWorkerProcessVO>> map = processVOList.stream().collect(Collectors.groupingBy(AppWorkerProcessVO::getMedia_type_id));

            for (Map.Entry<Integer, List<AppWorkerProcessVO>> entry : map.entrySet()) {
                AppWorkerProcessVO item = new AppWorkerProcessVO();
                for (AppWorkerProcessVO appWorkerProcessVO : entry.getValue()) {
                    item.setMedia_type_id(appWorkerProcessVO.getMedia_type_id());
                    item.setMedia_type_name(appWorkerProcessVO.getMedia_type_name());
                    item.setBuilding_area_count(null == item.getBuilding_area_count() ? appWorkerProcessVO.getBuilding_area_count() : appWorkerProcessVO.getBuilding_area_count() + item.getBuilding_area_count());
                    item.setCustomer_count(null == item.getCustomer_count() ? appWorkerProcessVO.getCustomer_count() : appWorkerProcessVO.getCustomer_count() + item.getCustomer_count());
                    item.setSubmit_media_point_count(null == item.getSubmit_media_point_count() ? appWorkerProcessVO.getSubmit_media_point_count() : appWorkerProcessVO.getSubmit_media_point_count() + item.getSubmit_media_point_count());
                    item.setTotal_media_point_count(null == item.getTotal_media_point_count() ? appWorkerProcessVO.getTotal_media_point_count() : appWorkerProcessVO.getTotal_media_point_count() + item.getTotal_media_point_count());
                }
                result.add(item);
            }
        }
        return ApiResult.ok(result);
    }

    @PostMapping("work-order-customer-detail")
    @ApiOperation("客户详情")
    public ApiResult<List<AppCustomerDetailResp>> workOrderCustomerDetail(@ApiParam("json") @RequestBody AppCustomerDetailReq req) {
        alterReqForUserId(req);

        List<Integer> allWorkOrderIdList = reportMapper.summaryTotalCount(req.getDateRangeType(), req.getUserId(), req.getWork_order_type_id());

        List<AppCustomerDetailResp> result = new ArrayList<>();
        if (allWorkOrderIdList.size() > 0) {
            List<WorkOrder> workOrders = workOrderService.listByIds(allWorkOrderIdList);
            List<Long> billSaleWorkOrderIdList = workOrders.stream().filter(item -> item.getBill_sale_id() != null).map(WorkOrder::getId).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(billSaleWorkOrderIdList)) {
                List<BillMarketShootRequirementExt> shootRequirementExtList = reportMapper.queryCustomerDetail(billSaleWorkOrderIdList, req.getMedia_type_id());
                if (CollUtil.isEmpty(shootRequirementExtList)) {
                    return ApiResult.ok(result);
                }
                Map<Long, List<BillMarketShootRequirementExt>> listMap = shootRequirementExtList.stream().collect(Collectors.groupingBy(BillMarketShootRequirementExt::getCustomer_id));
                for (Map.Entry<Long, List<BillMarketShootRequirementExt>> entry : listMap.entrySet()) {
                    List<BillMarketShootRequirementExt> requirementExtList = entry.getValue();
                    BillMarketShootRequirementExt requirementExt = requirementExtList.get(0);
                    AppCustomerDetailResp item = new AppCustomerDetailResp();
                    item.setCustomer_id(requirementExt.getCustomer_id());
                    item.setCustomer_name(requirementExt.getCustomer_name());
                    item.setShoot_requirement(assembleRequirement(requirementExtList));
                    result.add(item);
                }
            }
        }

        return ApiResult.ok(result);
    }

    /**
     * 针对需要userId的情况，前置设置
     */
    private <T extends AppReqUserId> void alterReqForUserId(T req) {
        Long userId = req.getUserId(); // 前端传入的userId

        if (userId == null) {
            // userId为空，则取本地线程中的
            userId = ThreadLocalUserUtil.get().getId();
        }

        if (AppUserTypeEnum.MANAGER.getId().equals(userService.getUserTypeForApp(userId))
                && req.getManageDefaultNull()) {
            // 用户是管理层，且该接口需要设置管理层userId为null
            req.setUserId(null);
        } else {
            req.setUserId(userId);
        }
    }

    private String assembleRequirement(List<BillMarketShootRequirementExt> requirementExtList) {
        StringBuilder result = new StringBuilder();
        if (CollUtil.isNotEmpty(requirementExtList)) {
            requirementExtList = requirementExtList.stream().filter(item -> item.getShoot_type() != null).collect(Collectors.toList());
            Map<String, List<BillMarketShootRequirementExt>> dateRangeGroup = requirementExtList.stream().collect(Collectors.groupingBy(BillMarketShootRequirementExt::dateRangeStr));
            List<String> keyList = dateRangeGroup.keySet().stream().sorted((str1, str2) -> -1 * str1.compareTo(str2)).collect(Collectors.toList());
            for (String key : keyList) {
                result.append(key).append("\n");
                for (BillMarketShootRequirementExt billMarketShootRequirementExt : dateRangeGroup.get(key)) {
                    result.append(billMarketShootRequirementExt.description());
                }
            }
        }
        return result.toString();
    }

    private List<AppWorkOrderCustomerProcessVO> assembleCustomerProcessList(List<AppWorkOrderReport> customerProcessList) {
        List<AppWorkOrderCustomerProcessVO> result = new ArrayList<>();
        Map<Integer, List<AppWorkOrderReport>> orderTypeListMap = customerProcessList.stream()
                .collect(Collectors.groupingBy(AppWorkOrderReport::getWork_order_type_id));
        for (Map.Entry<Integer, List<AppWorkOrderReport>> listEntry : orderTypeListMap.entrySet()) {
            Map<Long, List<AppWorkOrderReport>> customerListMap = listEntry.getValue().stream()
                    .collect(Collectors.groupingBy(AppWorkOrderReport::getWork_order_id));
            for (Map.Entry<Long, List<AppWorkOrderReport>> entry : customerListMap.entrySet()) {
                List<AppWorkOrderReport> workOrderList = entry.getValue();
                AppWorkOrderReport appWorkOrderReport = workOrderList.get(0);
                AppWorkOrderCustomerProcessVO item = new AppWorkOrderCustomerProcessVO();
                item.setWork_order_id(appWorkOrderReport.getWork_order_id());
                item.setDeadline(appWorkOrderReport.getDeadline());
                item.setWork_order_type_id(appWorkOrderReport.getWork_order_type_id());
                item.setWork_order_type_name(WorkOrderHelper.getWorkOrderName(appWorkOrderReport.getWork_order_type_id()));
                item.setCustomerId(appWorkOrderReport.getCustomer_id());
                item.setCustomerName(formatCustomerName(appWorkOrderReport));
                List<AppWorkOrderBuildingProcessVO> buildingProcessVOS = assembleBuildingProcessList(workOrderList, false);
                item.setBuildingProcessList(buildingProcessVOS);
                item.autoCalcCount();
                result.add(item);
            }
        }

        result.sort((o1, o2) -> {
            int o1workOrderTypeSort = WorkOrderHelper.getWorkOrderTypeSort(o1.getWork_order_type_id());
            int o2workOrderTypeSort = WorkOrderHelper.getWorkOrderTypeSort(o2.getWork_order_type_id());
            if (o1workOrderTypeSort != o2workOrderTypeSort) {
                return o1workOrderTypeSort - o2workOrderTypeSort;
            }
            //完成的往下面放
            if (!o1.getHasFinished().equals(o2.getHasFinished())) {
                return o1.getHasFinished() ? 1 : -1;
            }
            //按截止时间倒序
            return (int) (o2.getDeadline().toEpochDay() - o1.getDeadline().toEpochDay());
        });

        return result;
    }

    private String formatCustomerName(AppWorkOrderReport appWorkOrderReport) {
        return appWorkOrderReport.getCustomer_name() + "("
                + DateUtil.formatDate(appWorkOrderReport.getStart_at()) + "~" +
                DateUtil.formatDate(appWorkOrderReport.getEnd_at()) + ")";
    }

    private List<AppWorkOrderBuildingProcessVO> assembleBuildingProcessList(List<AppWorkOrderReport> buildingProcessList, boolean filter) {
        List<AppWorkOrderBuildingProcessVO> result = new ArrayList<>();

        Map<Integer, List<AppWorkOrderReport>> orderTypeListMap;
        if (filter) {
            Set<Integer> containWorkOrderTypeSet = CollUtil.newHashSet(1, 4, 5);
            orderTypeListMap = buildingProcessList.stream()
                    .filter(item -> containWorkOrderTypeSet.contains(item.getWork_order_type_id()))
                    .collect(Collectors.groupingBy(AppWorkOrderReport::getWork_order_type_id));
        } else {
            orderTypeListMap = buildingProcessList.stream()
                    .collect(Collectors.groupingBy(AppWorkOrderReport::getWork_order_type_id));
        }

        for (Map.Entry<Integer, List<AppWorkOrderReport>> entry : orderTypeListMap.entrySet()) {
            Map<Long, List<AppWorkOrderReport>> buildingListMap = entry.getValue().stream().collect(Collectors.groupingBy(AppWorkOrderReport::getBuilding_area_id));
            for (Map.Entry<Long, List<AppWorkOrderReport>> buildingEntry : buildingListMap.entrySet()) {
                AppWorkOrderReport appWorkOrderReport = buildingEntry.getValue().get(0);
                AppWorkOrderBuildingProcessVO item = new AppWorkOrderBuildingProcessVO();
                item.setWork_order_type_id(appWorkOrderReport.getWork_order_type_id());
                item.setWork_order_type_name(WorkOrderHelper.getWorkOrderName(appWorkOrderReport.getWork_order_type_id()));
                item.setBuilding_area_id(appWorkOrderReport.getBuilding_area_id());
                item.setBuilding_area_name(appWorkOrderReport.getBuilding_area_name());
                long hasSubmitCount = buildingEntry.getValue().stream().filter(s -> null != s.getWorker_submit_id()).count();
                long hasNotSubmitCount = buildingEntry.getValue().stream().filter(s -> null == s.getWorker_submit_id()).count();
                item.setHasSubmitCount(hasSubmitCount);
                item.setHasNotSubmitCount(hasNotSubmitCount);
                result.add(item);
            }
        }
        return result;
    }


}
